<template>
  <div id="globalHeader">
    <a-row :wrap="false">
<!--      头部栏-->
      <a-col flex="200px">
        <router-link to="/">
          <div class="title-bar">
            <img class="logo" src="https://cloud-picture-bank-1332739998.cos.ap-shanghai.myqcloud.com//space/1898947798724894722/2025-03-20_jd38XRVT9lruG42w.jpg" alt="图片协作平台 logo" />
            <div class="title">图片协作平台</div>
          </div>
        </router-link>
      </a-col>

<!--      导航栏-->
      <a-col flex="auto">
        <a-menu
          v-model:selectedKeys="current"
          mode="horizontal"
          :items="items"
          @click="doMenuClick"
        />
      </a-col>

<!--用户菜单展示栏-->
      <a-col flex="120px">
        <div class="user-login-status">
          <div v-if="loginUserStore.loginUser.id">
            <a-dropdown>
              <ASpace>
                <a-avatar :src="loginUserStore.loginUser.userAvatar" />
                {{ loginUserStore.loginUser.userName ?? '无名' }}
              </ASpace>
              <template #overlay>
                <a-menu>
<!--                  我的空间按钮-->
                  <a-menu-item>
                    <router-link to="/my_space">
                      <UserOutlined />
                      我的空间
                    </router-link>
                  </a-menu-item>

                  <!--个人信息按钮-->
                  <a-menu-item>
                    <router-link to="/my_detail">
                      <SolutionOutlined />
                      个人信息
                    </router-link>
                  </a-menu-item>


                  <!--修改密码按钮-->
                  <a-menu-item>
                    <router-link to="/editPassword">
                      <EditOutlined />
                        修改密码
                    </router-link>
                  </a-menu-item>

                  <!--注销按钮-->
                  <a-menu-item @click="doLogout">
                    <LogoutOutlined />
                    退出登录
                  </a-menu-item>

                </a-menu>
              </template>
            </a-dropdown>
          </div>

          <div v-else>
            <a-button type="primary" href="/user/login">登录</a-button>
          </div>
        </div>
      </a-col>
    </a-row>
  </div>
</template>
<script lang="ts" setup>
import { computed, h, ref } from 'vue'
import { HomeOutlined,LogoutOutlined,UserOutlined,SolutionOutlined,EditOutlined} from '@ant-design/icons-vue'
import { MenuProps, message } from 'ant-design-vue'
import { useRouter } from 'vue-router'
import { useLoginUserStore } from '@/stores/useLoginUserStore.ts'
import { userLogoutUsingPost } from '@/api/userController.ts'
import AddPicturePage from '@/page/AddPicturePage.vue'

//全局变量-当前登录用户
const loginUserStore = useLoginUserStore()

//当前要高亮的菜单项
const current = ref<string[]>([])

// 原始菜单列表
const originItems = [
  {
    key: '/',
    icon: () => h(HomeOutlined),
    label: '主页',
    title: '主页',
  },
  {
    key: '/add_picture',
    label: '图片投稿',
    title: '图片投稿',
  },
  {
    key: '/admin/pictureManage',
    label: '图片管理',
    title: '图片管理',
  },
  {
    key: '/admin/categoryTagManage',
    label: '分类标签',
    title: '分类标签',
  },
  {
    key: '/admin/spaceManage',
    label: '空间管理',
    title: '空间管理',
  },
  {
    key: '/admin/userManage',
    label: '用户管理',
    title: '用户管理',
  },
  {
    key: '/admin/vipCodeManage',
    label: '兑换码管理',
    title: '兑换码管理',
  },
  {
    key: '/user_exchange_vip',
    label: '兑换会员',
    title: '兑换会员',
  },
  {
    key: '/',
    label: h('a', { href: 'https://search.bilibili.com/all?keyword=%E9%99%88%E5%B9%B3%E5%AE%89&search_source=1', target: '_blank' }, '开发者 @ZZIQ'),
    title: '作者 zziq',
  },
]

// 过滤菜单项
const filterMenus = (menus = [] as MenuProps['items']) => {
  return menus?.filter((menu) => {
    if (menu?.key?.startsWith('/admin')) {
      const loginUser = loginUserStore.loginUser
      if (!loginUser || loginUser.userRole !== "admin") {
        return false
      }
    }
    // 如果用户未登录，过滤掉“兑换会员”菜单项
    if (menu?.key === '/user_exchange_vip' && !loginUserStore.loginUser.id) {
      return false;
    }
    return true
  })
}

// 展示在菜单的路由数组
const items = computed<MenuProps['items']>(() => filterMenus(originItems))
const router = useRouter()
// 路由跳转事件
const doMenuClick = ({ key }) => {
  router.push({
    path: key,
  })
}

// 用户注销
const doLogout = async () => {
  const res = await userLogoutUsingPost()
  console.log(res)
  if (res.data.code === 0) {
    loginUserStore.setLoginUser({
      userName: '未登录',
    })
    message.success('退出登录成功')
    await router.push('/user/login')
  } else {
    message.error('退出登录失败，' + res.data.message)
  }
}

// 监听路由变化，高亮当前菜单项
router.afterEach((to, from, next) => {
  current.value = [to.path]
})
</script>

<style scoped>
#globalHeader .title-bar {
  display: flex;
  align-items: center;
}

.title {
  color: black;
  font-size: 18px;
  margin-left: 16px;
}

.logo {
  height: 48px;
}
</style>
